<?php 

// Configure the email recieve

$your_email ='buituht@gmail.com';

$cc = 'buituht@gmail.com';



//Setup the variable



session_start();

$errors = '';

$companyname = '';

$contactname = '';

$phonenumber = '';

$faxnumber = '';

$postcode = '';

$address = '';

$visitor_email = '';

$url = '';

$user_message = '';



//If click the submit button

if(isset($_POST['submit']))

{

	$contactname = $_POST['contactname'];

	$address = $_POST['address'];

	$phonenumber = $_POST['phonenumber'];

    $visitor_email = $_POST['email'];

    $user_message = $_POST['message'];

	//------------Do Validations-------------

    /*

	if(empty($companyname)||empty($visitor_email))

    {

        $errors .= "\n Name and Email are required fields. ";    

    }

    if(IsInjected($visitor_email))

    {

        $errors .= "\n Bad email value!";

    }*/

    if(empty($_SESSION['captcha_image'] ) ||

      strcasecmp($_SESSION['captcha_image'], $_POST['captcha_image']) != 0)

    {

    //Note: the captcha code is compared case insensitively.

    //if you want case sensitive match, update the check above to

    // strcmp()

        $errors .= "\n The captcha code does not match!";

    }

    

    if(empty($errors))

    {

        //send the email

        $to = $your_email;

        $subject="Contact Message";

        $from = $your_email;

        $ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';

        

        $body = 

		

		"Email : $visitor_email\n".

		"Contact name : $contactname\n".

		"Phone number : $phonenumber \n".

		"Address : $address\n".

		"Message: $user_message\n".

        "IP: $ip\n";    

        

        $headers = "From: $from \r\n";

        $headers .= "Reply-To: $visitor_email \r\n";

        

        //mail($to, $subject, $body,$headers);

		//mail($cc, $subject, $body,$headers);

		

		mb_internal_encoding("UTF-8");

		mail($to,mb_encode_mimeheader($subject,'JIS','B'),mb_convert_encoding($body,'JIS'),$headers);

		mail($cc,mb_encode_mimeheader($subject,'JIS','B'),mb_convert_encoding($body,'JIS'),$headers);

		

		?>

       

		<script language="javascript">

			alert("Send successful! Thank you");

        	location.href="http://localhost/webdn/";

        </script><?php		

    }

	else{

	?><script language="javascript">alert("Error! Can't send");location.href="http://localhost/webdn/";</script><?php	

	header('Location: http://localhost/webdn/');



	}

}



// Function to validate against any email injection attempts

function IsInjected($str)

{

  $injections = array('(\n+)',

              '(\r+)',

              '(\t+)',

              '(%0A+)',

              '(%0D+)',

              '(%08+)',

              '(%09+)'

              );

  $inject = join('|', $injections);

  $inject = "/$inject/i";

  if(preg_match($inject,$str))

    {

    return true;

  }

  else

    {

    return false;

  }

}

?>